package com.woparker.kiosk.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.woparker.kiosk.utils.GlideUtilsKt;


/**
 * @author Mr.YuanXiaoDong
 * @ClassName: ViewHolder
 */
public class ViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;


    /**
     * <p>Title: </p>
     * <p>Description: </p>
     *
     * @param context
     * @param parent
     * @param layoutId
     * @param position
     * @param isLoadNetImage
     */
    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position, boolean isLoadNetImage) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        mConvertView.setTag(this);
    }

    /**
     * @param @param  context
     * @param @param  convertView
     * @param @param  parent
     * @param @param  layoutId
     * @param @param  position
     * @param @param  isLoadNetImage
     * @throws
     * @Title: get
     * @Description: TODO(ViewHolder)
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position, boolean isLoadNetImage) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder(context, parent, layoutId, position, isLoadNetImage);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            convertView.setTag(viewHolder);

        }
        return viewHolder;
    }

    /**
     * @param @return
     * @throws
     * @Title: getConvertView
     * @Description: TODO()
     */
    public View getConvertView() {
        return mConvertView;
    }

    /**
     * @param @param  viewId
     * @param @return
     * @return T
     * @throws
     * @Title: getView
     * @Description: TODO(ͨ)
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * @param @param  viewId
     * @param @param  text
     * @param @return
     * @return ViewHolder
     * @throws
     * @Title: setText
     * @Description: TODO()
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * @param @param  viewId
     * @param @param  drawableId
     * @param @return
     * @return ViewHolder
     * @throws
     * @Title: setImageResource
     * @Description: TODO()
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    /**
     * @param @param  cx
     * @param @param  viewId
     * @param @param  drawableId
     * @param @return
     * @return ViewHolder
     * @throws
     * @Title: setImageBackground
     * @Description: TODO( )
     */
    @SuppressWarnings("deprecation")
    public ViewHolder setImageBackground(Context cx, int viewId, int drawableId) {
        ImageView view = getView(viewId);

        view.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(cx.getResources(), drawableId)));
        //		view.setImageResource(drawableId);
        return this;
    }


    /**
     * @param @param  viewId
     * @param @param  bm
     * @param @return
     * @return ViewHolder
     * @throws
     * @Title: setImageBitmap
     * @Description: TODO()
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * @param @param  viewId
     * @param @param  url
     * @param @return
     * @return ViewHolder
     * @throws
     * @Title: setImageByUrl
     * @Description: TODO()
     */
    public ViewHolder setImageByUrl(int viewId, String url, int defoutlIconId, boolean isCache, int round) {
        if (!TextUtils.isEmpty(url)) {
            GlideUtilsKt.loadImageByUrl((ImageView) getView(viewId), url);
        } else {
            setImageResource(viewId, defoutlIconId);
        }
        return this;
    }

    /**
     * @param @return
     * @return int
     * @throws
     * @Title: getPosition
     * @Description: TODO()
     */
    public int getPosition() {
        return mPosition;
    }

}